package com.zzvcom.webapp.action.admin;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.zzvcom.persist.model.admin.SysModule;
import com.zzvcom.service.admin.SysModuleManager;
import com.zzvcom.utils.ExtMessage;

@Controller
@Scope("prototype")
public class SysModuleAction
{

    private SysModule module;

    private SysModuleManager manager;

    private ExtMessage msg = new ExtMessage();// 返回信息

    private Map rs = new HashMap();// 结果集合

    String parentid;

    public String getParentid()
    {
        return parentid;
    }

    public void setParentid(String parentid)
    {
        this.parentid = parentid;
    }

    public String execute()
    {

        DetachedCriteria dc = DetachedCriteria.forClass(SysModule.class);
        dc.addOrder(Order.asc("seq"));
        if (StringUtils.isBlank(parentid) || parentid.equals("0"))
        {
            dc.add(Restrictions.or(Restrictions.isNull("parentid"),
                    Restrictions.eq("parentid", "")));
        }
        else
        {
            dc.add(Restrictions.eq("parentid", parentid));
        }
        List list = manager.getListByDetachedCriteria(dc);

        rs.put("root", list);
        rs.put("records", list.size());
        return "rs";
    }

    public String save()
    {
        if (StringUtils.isBlank(module.getId()))
        {
            module.setId(null);
        }

        if (StringUtils.isBlank(module.getParentid()))
        {
            module.setParentid(null);
        }

        manager.save(module);
        msg.setInfo("保存成功");
        return "msg";
    }

    public String delete()
    {
        module = manager.get(module.getId());
        manager.delete(module);
        msg.setInfo("删除成功");
        return "msg";
    }

    List<Node> nodes = new ArrayList<Node>();

    String node;

    public String tree()
    {
        DetachedCriteria dc = DetachedCriteria.forClass(SysModule.class);
        dc.addOrder(Order.asc("seq"));
        if (StringUtils.isBlank(node) || node.equals("0"))
        {
            dc.add(Restrictions.or(Restrictions.isNull("parentid"),
                    Restrictions.eq("parentid", "")));
        }
        else
        {
            dc.add(Restrictions.eq("parentid", node));
        }
        Node node = null;
        for (SysModule m : (List<SysModule>) manager
                .getListByDetachedCriteria(dc))
        {
            node = new Node();
            node.setId(m.getId());
            node.setLeaf(false);
            node.setText(m.getName());
            node.setUrl(m.getUrl());
            nodes.add(node);
        }
        return "nodes";
    }

    List<AllNode> allnodes = new ArrayList<AllNode>();

    public String treeAll()
    {
        DetachedCriteria dc = DetachedCriteria.forClass(SysModule.class);
        dc.add(Restrictions.or(Restrictions.isNull("parentid"), Restrictions
                .eq("parentid", "")));
        dc.addOrder(Order.asc("seq"));
        AllNode node = null;
        for (SysModule m : (List<SysModule>) manager
                .getListByDetachedCriteria(dc))
        {
            node = formAllnode(m);
            allNodes(m.getId(), node);
            allnodes.add(node);
        }
        return "allnodes";
    }

    public SysModule getModule()
    {
        return module;
    }

    public void setModule(SysModule module)
    {
        this.module = module;
    }

    public SysModuleManager getManager()
    {
        return manager;
    }

    @Autowired
    public void setManager(SysModuleManager manager)
    {
        this.manager = manager;
    }

    public ExtMessage getMsg()
    {
        return msg;
    }

    public void setMsg(ExtMessage msg)
    {
        this.msg = msg;
    }

    public Map getRs()
    {
        return rs;
    }

    public void setRs(Map rs)
    {
        this.rs = rs;
    }

    public static class Node
    {
        private String id;

        private String text;

        private boolean leaf = false;

        private String url;

        public String getId()
        {
            return id;
        }

        public void setId(String id)
        {
            this.id = id;
        }

        public String getText()
        {
            return text;
        }

        public void setText(String text)
        {
            this.text = text;
        }

        public boolean isLeaf()
        {
            return leaf;
        }

        public void setLeaf(boolean leaf)
        {
            this.leaf = leaf;
        }

        public String getUrl()
        {
            return url;
        }

        public void setUrl(String url)
        {
            this.url = url;
        }

    }

    public static class AllNode extends Node
    {
        List<Node> children = new ArrayList<Node>();

        public List<Node> getChildren()
        {
            return children;
        }

        public void setChildren(List<Node> children)
        {
            this.children = children;
        }
    }

    private void allNodes(String _parentid, AllNode root)
    {
        if (root == null)
        {
            return;
        }
        DetachedCriteria dc = DetachedCriteria.forClass(SysModule.class);
        dc.addOrder(Order.asc("seq"));
        dc.add(Restrictions.eq("parentid", _parentid));
        AllNode anode = null;
        for (SysModule m : (List<SysModule>) manager
                .getListByDetachedCriteria(dc))
        {
            anode = formAllnode(m);
            root.getChildren().add(anode);
            if (!root.isLeaf())
            {
                allNodes(m.getId(), anode);
            }
        }
    }

    private AllNode formAllnode(SysModule m)
    {
        AllNode anode = new AllNode();
        anode = new AllNode();
        anode.setId(m.getId());
        anode.setText(m.getName());
        anode.setUrl(m.getUrl());
        anode.setLeaf(Integer.valueOf(manager.queryUniqueByHQL(
                "select count(*) from SysModule where parentid=? ", m.getId())
                .toString()) == 0);
        return anode;
    }

    public List<Node> getNodes()
    {
        return nodes;
    }

    public void setNodes(List<Node> nodes)
    {
        this.nodes = nodes;
    }

    public String getNode()
    {
        return node;
    }

    public void setNode(String node)
    {
        this.node = node;
    }

    public List<AllNode> getAllnodes()
    {
        return allnodes;
    }

    public void setAllnodes(List<AllNode> allnodes)
    {
        this.allnodes = allnodes;
    }

}
